package com.bite.common.core.domain;

import com.bite.common.core.enums.ResultCode;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class R<T> {
    private int code;   // 定义一组固定的code
    private String msg; // 通常是code的辅助说明
    private T data;     // 请求某个接口，接口返回的数据

    private R(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.msg = resultCode.getMsg();
        data = null;
    }

    private R(ResultCode resultCode, T data) {
        this.code = resultCode.getCode();
        this.msg = resultCode.getMsg();
        this.data = data;
    }

    public static <T> R<T> ok(){
        return new R<>(ResultCode.SUCCESS);
    }

    public static <T> R<T> ok(T data){
        return new R<>(ResultCode.SUCCESS, data);
    }

    public static <T> R<T> fail(ResultCode resultCode){
        return new R<>(resultCode);
    }

    public static <T> R<T> fail(ResultCode resultCode, T data){
        return new R<>(resultCode, data);
    }

}
